/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import fr.ove.openmath.mathematica.*;
import fr.ove.openmath.mathematica.events.*;

public class BracketBullet extends JPanel {
    /**
    * The PanelJomes the instance will contain. 
    */
    private PanelJomes panelJomes;
    
    /**
    * The width of the bracket line displayed.
    */
    private static final int width = 8;
    
    /**
    * The minimal of the instance.
    */
    int minHeight = 15;
    
    /**
    * Chesks if the instance is selected or not. <CODE>true</CODE> by default.
    */
    boolean isSelected = true;
    
    /**
    * The Constructor.
    * @panel pj the PanelJomes contained by the instance.
    */
    public BracketBullet(PanelJomes pj) {
        panelJomes = pj;
        
        Font font = pj.getJomeRequest().getFont();
        FontMetrics fm = getFontMetrics(font);
        minHeight = fm.getHeight() + 4;
        
        addMouseListener(
            new MouseAdapter() {
                public void mousePressed(MouseEvent event) {
                	PanelJomesEvent panelJomesEvent = new PanelJomesEvent(panelJomes);
                	panelJomesEvent.setAction(PanelJomesEvent.UPDATE, null);
                	panelJomes.firePanelJomesEvent(panelJomesEvent);
                	    
                	panelJomesEvent.setAction(PanelJomesEvent.UPDATE_SELECTION, null);
                	panelJomes.firePanelJomesEvent(panelJomesEvent);
                }
            }
        );
    }
    
    /**
    * Returns the preferred size of the instance.
    */
    public Dimension getPreferredSize() {
        return new Dimension(width, (int) Math.max(minHeight, (panelJomes.getSize()).height));
    }
    
    /**
    * Checks if the instance is selected.
    * @param <CODE>true</CODE> if the instance is selected. <CODE>false</CODE> otherwise.
    */
    public boolean isSelected() {
        return isSelected;
    }
    
    /**
    * Sets the instance as selected or not.
    * @param isSelected the selection sate.
    */
    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
        repaint();
    }
    
    /**
    * Paints the instance.
    * @param g where to paint.
    */
    public void paint(Graphics g) {
        Dimension size = getPreferredSize();
        if (isSelected)
            g.setColor(Color.red);
        else
            g.setColor(Color.black);
            
        g.fillRect(4, 2, 2, size.height - 4);
        g.fillRect(4, 2, width, 2);
        g.fillRect(4, size.height - 4, width, 2);
    }
}
